package com.lkf.simplewebcontainer.servlet;

import com.lkf.simplewebcontainer.http.Request;
import com.lkf.simplewebcontainer.http.Response;
import io.netty.handler.codec.http.HttpResponseStatus;

import java.util.List;
import java.util.Map;

/**
 * 对SimpleWebContainer的Servlet规范的简单实现
 */
public class DefaultServlet extends Servlet {
    @Override
    public void doGet(Request request, Response response) throws Exception {
        String uri = request.getUri();
        String path = request.getPath();
        String method = request.getRequestMethod();
        Map<String, List<String>> parameters = request.parameters();
        String content = "客户端请求信息如下：\n" + "uri=" + uri + "\npath=" + path + "\nmethod=" + method + "\n";
        for (Map.Entry<String, List<String>> entry : parameters.entrySet()) {
            content += "param=" + entry.getKey() + "&value=" + entry.getValue() + "\n";
        }
        response.write(content, HttpResponseStatus.OK, "text/json");
    }

    @Override
    public void doPost(Request request, Response response) throws Exception {
        doGet(request, response);
    }
}
